package twu.shubham.library;

import java.util.Scanner;

public class Biblioteca {

    public static void main(String args[])
    {
        int toContinue = 1;
        int menuOption;
        int totalBooks =5;
        int totalMovies = 15;
        int totalMembers = 5;
        int loginStatus = 0;
        int loginId = 0;
        BookRecord[]  bookList =new BookRecord[totalBooks];
        bookValues(bookList);
        movieRecord[] movieList = new movieRecord[totalMovies];
        movieValues(movieList);
        MembersRecord[] membersList = new MembersRecord[totalMembers];
        membersValues(membersList,totalMembers);

        System.out.println("Welcome to Library system");

        while (toContinue==1)
        {
            System.out.println("Menu : \n" +
                    "1.See all the books\n" +
                    "2.See all the movies\n" +
                    "3.Return a book\n" +
                    "4.See the membership detail\n" +
                    "5.Login\n"+
                    "6.Logout\n"+
                    " enter an option");
            int bookNumber;
            Scanner input = new Scanner( System.in );
            menuOption = input.nextInt();

            if(menuOption==1)
            {
                  for(int i=0; i<totalBooks;i++)
                      bookList[i].bookPrint(i+1);

                System.out.println("Want to reserve a book : 1.Yes\n2.No\n");
                int reserveBook = input.nextInt();

                if(reserveBook==1)
                {
                    if(loginStatus==1)
                    {
                        System.out.println("Enter the book number : ");
                        bookNumber = input.nextInt();

                        if(bookNumber<=totalBooks && bookNumber-1>=0)
                        {
                            bookList[bookNumber-1].reserveBook(bookNumber);

                        }

                        else
                        {
                            System.out.println("Not a Valid book number\n");
                        }
                    }

                    else
                    {
                        System.out.println("Login First !!!");
                    }
                }

            }

            if(menuOption==2)
            {
                for(int i=0; i<totalMovies;i++)
                    movieList[i].moviePrint(i + 1);
            }

            if(menuOption==3)
            {
                if(loginStatus==1)
                {
                    System.out.println("Enter the book number");
                    bookNumber=input.nextInt();
                    if(bookNumber<=totalBooks && bookNumber-1>=0)
                    {
                        bookList[bookNumber-1].returnBook();
                    }
                    else
                    {
                        System.out.println("Not a valid book number");
                    }
                }
                else
                {
                    System.out.println("Login First !!!");
                }
            }

            if(menuOption==4)
            {
                if(loginStatus==0)
                {
                    System.out.println("Login First !!!");
                }

                else
                {
                    int done=0;
                    for(int i=0;i<totalMembers && done==0;i++)
                    {
                        if(membersList[i].libraryNumber==loginId)
                        {
                            membersList[i].printDetail();
                            done=1 ;
                        }
                    }
                }
            }

            if(menuOption==5)
            {
                {
                    if(loginStatus==0)
                    {
                        System.out.println(" Enter Library number : ");
                        int libraryNumber=input.nextInt();
                        for(int i=0;i<totalMembers && loginStatus==0;i++)
                        {
                            if(membersList[i].libraryNumber==libraryNumber)
                            {
                                System.out.println("Enter Password : ");
                                String password = input.next();
                                int a = membersList[i].checkForPassword(password);
                                if(a==1)
                                {
                                    loginStatus=1;
                                    loginId=libraryNumber;
                                }
                            }


                        }
                    }
                    else
                    {
                        System.out.println("Already Logged in\n");
                    }
                    if(loginStatus==0)
                    {
                        System.out.println("Either Wrong library number or Password\n");
                    }

                }
            }

            if(menuOption==6)
            {
                if(loginStatus==1)
                {
                    loginStatus=0;
                    loginId=0;
                    System.out.println("Thank you\n");
                }
                else
                {
                    System.out.println("No user is logged in\n");
                }
            }



            System.out.println("Do You want to continue : 1.Yes\t2.No\n");
            toContinue=input.nextInt();

        }
    }

    private static void membersValues(MembersRecord membersList[],int totalMembers) {

        membersList[0]= new MembersRecord(1111111,"Amit","551122","amit@gmail.com") ;
        membersList[1]= new MembersRecord(1111112,"Ankit","554432","ankit@gmail.com") ;
        membersList[2]= new MembersRecord(1111113,"Rohit","554434","rohit@gmail.com") ;
        membersList[3]= new MembersRecord(1111114,"Abhi","554433","abhi@gmail.com") ;
        membersList[4]= new MembersRecord(1111115,"Ankita","554436","ankita@gmail.com") ;
        for(int i = 0;i<totalMembers;i++)
        {
               membersList[i].passwordGenerator();
        }
    }

    private static void bookValues(BookRecord bookList[]) {

        bookList[0] = new BookRecord(2,"a");
        bookList[1] = new BookRecord(0,"b");
        bookList[2] = new BookRecord(1,"c");
        bookList[3] = new BookRecord(2,"d");
        bookList[4] = new BookRecord(4,"e");
    }

    private static void movieValues(movieRecord movieList[]) {

        movieList[0] = new movieRecord("Sholay",1975,"R.Sippy",7);
        movieList[1] = new movieRecord("Guide",2008,"R.Mehra",7);
        movieList[2] = new movieRecord("Satya",1976,"P.Sippy",7);
        movieList[3] = new movieRecord("Tiranga",1977,"S.Sippy",7);
        movieList[4] = new movieRecord("Anand",1978,"T.Sippy",7);
        movieList[5] = new movieRecord("Aamir",2008,"R.Mehra",7);
        movieList[6] = new movieRecord("Chennai",2008,"R.Mehra",7);
        movieList[7] = new movieRecord("Express",2008,"R.Mehra",7);
        movieList[8] = new movieRecord("Dabbang",2008,"R.Mehra",7);
        movieList[9] = new movieRecord("Wanted",2008,"R.Mehra",7);
        movieList[10] = new movieRecord("Players",2008,"R.Mehra",7);
        movieList[11] = new movieRecord("Black",2008,"R.Mehra",7);
        movieList[12] = new movieRecord("xyzzz",2008,"R.Mehra",7);
        movieList[13] = new movieRecord("Maachis",2008,"R.Mehra",7);
        movieList[14] = new movieRecord("Krati",2008,"R.Mehra",7);


    }
}
